package com.barunsw.bwf.bims.channel_history;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.barunsw.bwf.bims.vo.ChannelHistoryVo;
import com.barunsw.bwf.bims.vo.ChannelVo;
import com.barunsw.bwf.common.vo.ResultVo;
import com.barunsw.bwf.constants.Result;

@Controller
@RequestMapping("/channel_history")
public class ChannelHistoryController {
	private static Logger LOGGER = LoggerFactory.getLogger(ChannelHistoryController.class);
	
	@Autowired
	ChannelHistoryService channelHistoryService;
	
	@RequestMapping(value="/{channelId}", method=RequestMethod.GET)
	public @ResponseBody List<ChannelHistoryVo> getChannelHistoryById(@RequestBody @PathVariable long channelId,
			@RequestBody @RequestParam(value="count", required=false) long count,
			@RequestBody @RequestParam(value="startTime", required=false) String startTime,
			@RequestBody @RequestParam(value="endTime", required=false) String endTime) throws Exception {
		
		LOGGER.debug(String.format("count=%s, startTime=%s, endTime=%s", count, startTime, endTime));
		
		List<ChannelHistoryVo> channel = channelHistoryService.getHistoryList(channelId, count, startTime, endTime);
		return channel;
	}
	
	@RequestMapping(value="/{channelId}", method=RequestMethod.POST)
	public @ResponseBody ResultVo insertChannelHistory(@RequestBody ChannelHistoryVo channelHistoryVo) throws Exception {
		LOGGER.debug("insertChannelHistory");
		
		ResultVo resultVo = new ResultVo(Result.OK, "");
		
		int result = channelHistoryService.insertHistory(channelHistoryVo);
		if ( result <= 0 ) {
			resultVo.setResult(Result.NOK);
			resultVo.setReason("Channel History 추가시 오류가 발생하였습니다.");
		}
		
		return resultVo;
	}
}
